package com.zrk.generic;

import java.util.ArrayList;
import java.util.List;

public class UseGenericLearn {
    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>();
        Person xiaoZhang = new Person("xiao zhang", 13);
        Person xiaoMing = new Person("xiao Ming", 12);
        personList.add(xiaoZhang);
        personList.add(xiaoMing);
        System.out.println(personList);
        personList.sort(Person::compareTo);
        System.out.println(personList);
    }
}

/**
 * 实现Comparable接口并复写方法后可以使用sort对数组按规则进行排序
 */
class Person implements Comparable<Person>{
    String name;
    int age;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Person other) {
        // return this.name.compareTo(other.name);
        return this.age - other.age;
    }
}
